using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Esercitazione_CG_1
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        CameraSplitScreen camera;
        public ModelManagerSplitScreen modelmanager;
        Viewport defaultViewport, leftViewport, rightViewport;
        Matrix halfprojectionMatrix;
        MenuHelper menuHelper;

        public Game1()
        {
            //var ciao = new MenuHelper(this);
            graphics = new GraphicsDeviceManager(this);

            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            GraphicsDevice.RasterizerState = RasterizerState.CullNone;

            halfprojectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, 2.0f / 3.0f, 1.0f, 10000f);
            defaultViewport = graphics.GraphicsDevice.Viewport;
            leftViewport = defaultViewport;
            rightViewport = defaultViewport;
            leftViewport.Width = leftViewport.Width / 2;
            rightViewport.Width = rightViewport.Width / 2;
            rightViewport.X = leftViewport.Width;

            var positionCameras = new Vector3[]{new Vector3(0, 100, 500), 
                                                new Vector3(0,100,-500)};

            var rotationCameras = new Vector3[]{new Vector3(0, -MathHelper.PiOver4 / 4, 0), 
                                                new Vector3(MathHelper.Pi, -MathHelper.PiOver4 / 4, 0)};



            menuHelper = new MenuHelper(this, defaultViewport);
            Components.Add(menuHelper);

            camera = new CameraSplitScreen(this, positionCameras, rotationCameras, halfprojectionMatrix, rightViewport, leftViewport);
            Components.Add(camera);

            modelmanager = new ModelManagerSplitScreen(this, camera, leftViewport, rightViewport);
            Components.Add(modelmanager);

            base.Initialize();
        }

        protected override void LoadContent()
        {
            modelmanager.add("casetta del micio", Matrix.CreateRotationX(-MathHelper.PiOver2) * Matrix.CreateScale(7));
            modelmanager.add("GATTO", Matrix.CreateRotationX(-MathHelper.PiOver2) * Matrix.CreateScale(3) * Matrix.CreateTranslation(new Vector3(30, 10, 30)));
        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
                Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            //Disegno i modelli
            modelmanager.Draw();

            //Disegno le camere
            camera.Draw();

            //Disegno il menu helper
            menuHelper.draw();

            base.Draw(gameTime);

        }

    }
}